<?php
class DBConnPoolFactory {
	private static $instance;
	private $connPools;
	private $farmConnPools;
	
	private function __construct() {
		$this->connPools = array();
		$this->farmConnPools = array();
	}
	
	static public function getInstance() {
		if (!isset(self::$instance)) {
			$c = __CLASS__;
			self::$instance = new $c();
		}
		
		return self::$instance;	
	}

	// Prevent users to clone the instance
	public function __clone() {
		trigger_error('Clone is not allowed.', E_USER_ERROR);
	}
	
	public function getConnPool($configFile) {
		if (!isset($this->connPools[$configFile])) {
			$this->connPools[$configFile] = new DBConnPool($configFile);
		}
		
		return $this->connPools[$configFile];	
	}
	
	public function clearConnPool() {
		foreach ($this->connPools as $connPool) {
			$connPool->clearPool();
		}
	}
	
	public function getFarmConnPool($configFile) {
		if (!isset($this->farmConnPools[$configFile])) {
			$this->farmConnPools[$configFile] = new DBFarmConnPool($configFile);
		}
		
		return $this->farmConnPools[$configFile];
	}
	
	public function clearFarmConnPool() {
		foreach ($this->farmConnPools as $connPool) {
			$connPool->clearPool();
		}
	}
}
?>
